const router = require('koa-router')();
router.prefix(appConfig.authPrefixUrl+'/desktop/carnum_datas_dept_type');
const CarnumDatasDeptTypeService = appRequire('service/desktop/CarnumDatasDeptTypeService.js');
const ResultVo = require(appConfig.rootUrl+'/vo/ResultVo.js');

router.post('/save',async(ctx)=>{
  try{
    let carnumDatasDeptType = ctx.request.body;
    let id = await CarnumDatasDeptTypeService.save(carnumDatasDeptType);
    ctx.body = ResultVo.success('success',id);
  }catch(err){
    //集中处理错误
    err.msg = "新增车主部门失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/deleteOne',async(ctx)=>{
  try{
    let carnumDatasDeptType = ctx.query;
    carnumDatasDeptType = await CarnumDatasDeptTypeService.deleteOne(carnumDatasDeptType.id);
    ctx.body = ResultVo.success('success',carnumDatasDeptType);
  }catch(err){
    //集中处理错误
    err.msg = "删除车主部门失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.post('/deleteMany',async(ctx)=>{
  try{
    let param = ctx.request.body;
    console.log(param);
    param = await CarnumDatasDeptTypeService.deleteMany(param.ids);
    ctx.body = ResultVo.success('success',param);
  }catch(err){
    //集中处理错误
    err.msg = "删除车主部门失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findOne',async(ctx)=>{
  try{
    let carnumDatasDeptType = ctx.query;
    carnumDatasDeptType = await CarnumDatasDeptTypeService.findOne(carnumDatasDeptType.id);

    console.log(carnumDatasDeptType);
    ctx.body = ResultVo.success('success',carnumDatasDeptType);
  }catch(err){
    //集中处理错误
    err.msg = "查找车主部门失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findAllByType',async(ctx)=>{
  try{
    let carnumDatasDeptType = ctx.query;
    let data = await CarnumDatasDeptTypeService.findAllByType(carnumDatasDeptType);
    ctx.body = ResultVo.success('success',data);
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取全部车主部门数据失败!');
  }
});
router.get('/findAll',async(ctx)=>{
  try{
    let data = await CarnumDatasDeptTypeService.findAll();
    ctx.body = ResultVo.success('success',data);
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取全部车主部门数据失败!');
  }
});
router.get('/findByPage',async(ctx)=>{
  try{
    let page = ctx.query;
    console.log(page);
    let data = await CarnumDatasDeptTypeService.findByPage(page);
    ctx.body = data;
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取分页车主部门数据失败!');
  }
});
router.get('/initTableData',async(ctx)=>{
  try{
    let data = await CarnumDatasDeptTypeService.initTableData();
    ctx.body = data;
  }catch(error){
    // ctx.body = ResultVo.error(-1,'初始化车主部门数据失败!');
     //集中处理错误
    error.msg = "初始化车主部门数据失败";
    ctx.app.emit('error', error, ctx);
  }
});
module.exports = router;